package main

import (
	"fmt"
	"gitee.com/alex_li/weight/internal"
	"gitee.com/alex_li/weight/internal/etc"
	"github.com/lishimeng/app-starter"
	shutdown "github.com/lishimeng/go-app-shutdown"
	"github.com/lishimeng/go-log"
	"time"
)

func main() {
	var err error

	defer func() {
		if err := recover(); err != nil {
			fmt.Println(err)
		}
	}()

	err = _main()
	if err == nil {
		shutdown.WaitExit(&shutdown.Configuration{
			BeforeExit: func(s string) {
				log.Info(s)
			},
		})
	} else {
		shutdown.Exit(err.Error())
	}
	if err != nil {
		log.Info(err)
	}
	time.Sleep(time.Second * 2)
}

func _main() (err error) {
	configName := "config.toml"
	ctx := shutdown.Context()

	application := app.New(ctx)

	err = application.LoadConfig(&etc.Config, configName, ".", "/etc/weight")
	if err != nil {
		return
	}

	log.Info("start [%s]", etc.Config.Name)
	err = internal.Setup(ctx)
	if err != nil {
		return
	}

	err = application.Start()

	return
}